//
//  ZDDStatuses.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/27.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit

class ZDDStatuses: NSObject {
 var created: String?
    /// 微博创建时间
    var created_at: String?{
        didSet{

        created = (self.created_at! as NSString).substringWithRange(NSMakeRange(10, 9))
        }
    
    }
    /// 微博ID
    var id: Int64 = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?{
        didSet{
        sourceAttr = getSouceStr(source)
        }
    }
    //用户信息
    var user:ZDDUser?
    //  微博来源
    var sourceAttr: NSMutableAttributedString?
    // 转发微博
    var retweeted_status:ZDDStatuses?
    //	int	转发数
    var repostsStr: String?
    //	int	评论数
    var commentsStr: String?
    //	int	表态数
    var attitudesStr: String?
    //配图数组
    var pic_urls:[ZDDPictureModel]?
    //转发数
    var reposts_count:Int = 0{
    
        didSet{
            repostsStr = getCountString(reposts_count,title: "转发")
        }
    }
    //评论
    var comments_count:Int = 0{
        
        didSet{
            commentsStr = getCountString(comments_count,title: "评论")
        }
    }
    //表态
    var attitudes_count:Int = 0{
        
        didSet{
            attitudesStr = getCountString(attitudes_count,title: "赞")
        }
    }
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
        
            guard let v = value as?[String:AnyObject] else{
                return
            }
            let u = ZDDUser(dict: v)
            
            user = u
        
        }else if key == "retweeted_status"{
            guard let v = value as?[String:AnyObject] else{
            
            return
            }
            let u = ZDDStatuses(dict: v)
            retweeted_status = u
            
        }else if key == "pic_urls" {
            guard let v = value as? [[String:AnyObject]] else{
            
                return
            }
            //创建一个临时数组
            var tempArray : [ZDDPictureModel] = [ZDDPictureModel]()
            for dict in v {
            let p = ZDDPictureModel(dict: dict)
                tempArray.append(p)
            }
            pic_urls = tempArray
            
        }else{
        super.setValue(value, forKey: key)
        }
    }
    
    func getCountString(count: Int,title:String) -> String{
    
        if count <= 0 {
        return title
        }else if count > 0 && count < 10000 {
            
           
        return "\(count)"
        
        }else{
            
        let countF = CGFloat(count/1000)/10

        var countStr  = "\(countF)"
            if countStr.containsString(".0"){
            
                countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            }
            return "\(countF)万"
            
        }
    }
    //微博来源
    func getSouceStr(source:String?)->NSMutableAttributedString{
        
        guard let s = source where s.containsString("\">") else{
            
            let r = "来自 那菊花盛开的地方"
            return getSourceAttr(r)
        }
        let start = s.rangeOfString("\">")!
        let endStr = s.rangeOfString("</a")!
        
        let result = s.substringWithRange(start.endIndex..<endStr.startIndex)
        
        //字符串的拼接
        let r = "来自\(result)"
        return getSourceAttr(r)
        
    }
    // 通过字符串生成富文本
    func getSourceAttr(result:String)->NSMutableAttributedString{
        
        let attr = NSMutableAttributedString(string: result)
        
        //获取range
        let range = (result as NSString).rangeOfString("来自")
        
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor], range: range)
        
        return attr
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
}
